<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      title="查询"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="发生日期">
              <el-date-picker
                v-model="dateRange"
                size="small"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                style="width: 240px"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :clearable="false"
                :picker-options="pickerOptions"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营销单号" prop="molSodh">
              <el-input
                v-model="queryParams.molSodh"
                placeholder="请输入营销单号"
                clearable
                style="width: 240px"
                size="small"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制造工单" prop="moeZzdh">
              <el-input
                v-model="queryParams.moeZzdh"
                placeholder="请输入制造工单"
                clearable
                style="width: 240px"
                size="small"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制造物料客户代码" prop="itmPmgg">
              <el-input
                v-model="queryParams.itmPmgg"
                placeholder="请输入制造物料客户代码"
                clearable
                style="width: 240px"
                size="small"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营销物料客户代码" prop="somItmPmgg">
              <el-input
                v-model="queryParams.somItmPmgg"
                placeholder="请输入营销物料客户代码"
                clearable
                style="width: 240px"
                size="small"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel"
          size="small"
          >明细数据</el-button
        >
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="制工单盈亏明细" name="first">
        <el-table
          border
          stripe
          id="inOutDetail"
          :height="tableHChild"
          :data="
            moneyMzProfitList.slice(
              (currentPage - 1) * pageSize,
              currentPage * pageSize
            )
          "
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="销售单号"
            align="center"
            width="110px"
            prop="molSodh"
          />
          <el-table-column
            label="销售物料代码"
            align="center"
            width="100px"
            prop="somWldm"
          />
          <el-table-column
            label="销售物料品名规格"
            align="center"
            width="250px"
            :show-overflow-tooltip="true"
            prop="somItmPmgg"
          />
          <el-table-column label="营销数量" align="center" prop="molSomDdsl" />
          <el-table-column
            label="制造工单"
            align="center"
            width="110px"
            prop="molZzdh"
          />
          <el-table-column
            label="制造物料代码"
            align="center"
            width="100px"
            prop="molWldm"
          />
          <el-table-column
            label="制造物料品名规格"
            align="center"
            width="250px"
            :show-overflow-tooltip="true"
            prop="itmPmgg"
          />
          <el-table-column label="制造数量" align="center" prop="molMzDdsl" />
          <el-table-column
            label="领料总费用"
            width="90px"
            align="center"
            prop="molLlMoney"
          />
          <el-table-column
            label="入库产品折算费用"
            width="130px"
            align="center"
            prop="molRkMoney"
          />
          <el-table-column
            label="制造工单材料盈亏"
            width="130px"
            align="center"
            prop="molMzMaterialMoney"
          >
            <template #default="scope">
              <a
                style="text-decoration: underline; color: blue"
                @click="handleClick(scope.row, 1)"
                >{{ scope.row.molMzMaterialMoney }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            label="标准工单耗时"
            width="130px"
            align="center"
            prop="molCxsj"
          />
          <el-table-column
            label="实际工单耗时"
            width="130px"
            align="center"
            prop="molSjCxsj"
          />
          <el-table-column
            label="标准加工费"
            width="130px"
            align="center"
            prop="molProcessingFees"
          />
          <el-table-column
            label="制造工单加工盈亏"
            width="130px"
            align="center"
            prop="molMzProcessingMoney"
          >
            <template #default="scope">
              <a
                style="text-decoration: underline; color: blue"
                @click="handleClick(scope.row, 2)"
                >{{ scope.row.molMzProcessingMoney }}</a
              >
            </template>
          </el-table-column>
          <el-table-column
            label="生产部盈亏"
            width="130px"
            align="center"
            prop="molProduceMoney"
          />
        </el-table>
        <div>
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="pageSize"
            layout="total,prev,pager,next"
            :total="moneyMzProfitList.length"
          ></el-pagination>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!-- 盈亏明细   -->
    <el-dialog
      title="盈亏明细"
      v-model="openMoney"
      :close-on-click-modal="false"
      width="80%"
      append-to-body
    >
      <MoneyMaterialProfit
        v-if="typeYH === 1"
        :query="queryMoneyMaterialProfit"
      />
      <ProcessProfit v-if="typeYH === 2" :query="queryMoneyMaterialProfit" />
    </el-dialog>
  </div>
</template>

<script>
import {
  exportTotalMz,
  getMakeOrderProfitAndLoss,
} from "@/api/money/moneyMzlProfit";
import MoneyMaterialProfit from "../moneyMaterialProfit";
import ProcessProfit from "../moeProcessProfit";

export default {
  name: "MoneyMzlProfit",
  components: { MoneyMaterialProfit, ProcessProfit },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 表格高度
      tableHChild: document.body.clientHeight - 250,
      // 首选项
      activeTab: "first",
      // 明细 OR Total
      moneyMzProfitList: [],
      moneyMaterialProfitTotalList: [],
      // 显示搜索条件
      openSearch: false,
      openMoney: false,
      currentPage: 1,
      pageSize: 20,

      // 建立日期时间范围
      dateRange: [
        this.moment().add(-1, "month").startOf("month").toDate(),
        this.moment().add(-1, "month").endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        moeZzdh: null,
        moeJtbh: null,
        itmPmgg: null,
        somItmPmgg: null,
        beginTime: this.moment().add(-1, "month").format(),
        endTime: this.moment().format(),
        index: null,
      },
      queryMoneyMaterialProfit: {},
      typeYH: 0,
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.beginTime = this.moment(this.dateRange[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.endTime = this.moment(this.dateRange[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      getMakeOrderProfitAndLoss(this.queryParams).then((response) => {
        //明细
        this.moneyMzProfitList = response.data;
        this.loading = false;
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    handleSearch() {
      this.openSearch = true;
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.moneyMzProfitList = [];
      this.getList();
      this.openSearch = false;
    },
    //导出Excel
    handleExcel() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportTotalMz(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    handleClick(row, type) {
      this.openMoney = true;
      this.typeYH = type;
      this.queryMoneyMaterialProfit = {
        ...this.queryParams,
        moeZzdh: row.molZzdh,
        molSodh: row.molSodh,
        params: { beginDate: this.queryParams.beginTime },
        params: { endDate: this.queryParams.endTime },
      };

      console.log(row);
      return;
    },
  },
};
</script>

<style lang="scss" scoped></style>
